<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 下午2:45
 */
class Translators extends CI_Controller
{
    /**
     * 翻译员领取任务
     */
    public function receiveTask()
    {
        if (!Permission::getTask()) {
            exit(json_encode('Error : 你无权限领取任务 !'));
        }
        $per = $this->lyw_common_model->getTaskPermission();
        if ($per['code'] == 2) {
            exit(json_encode('Error : 一次只能领取一个任务'));
        } elseif ($per['code'] == 3) {
            exit(json_encode('提醒 : 由于翻译未在24小时内完成,现在24小时内禁止领取新任务,解锁时间为' . date('Y/m/d H:i:s', $per['receive_time'])));
        } elseif ($per['code'] == 4) {
            exit(json_encode('提醒 : 24小时内只能领取一个任务!请于:' . date('Y/m/d H:i:s', $per['receive_time']) . '后再领取任务!'));
        }
        $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : 0;
        $manager_id = isset($_REQUEST['manager_id']) ? $_REQUEST['manager_id'] : 0;
        if (empty($user_id) || empty($manager_id)) {
            exit('Error : empty information');
        }
        $this->load->model('lyw_article_manager_model');
        if ($this->lyw_article_manager_model->receivedStatus($user_id, $manager_id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('Error : mysql error !'));
        }
    }

    public function getMyTaskJson()
    {
        $page = isset($_POST['page']) ? $_POST['page'] : 0;
        $rows = isset($_POST['rows']) ? $_POST['rows'] : 0;
        sessionOpen();
        $this->load->model('Lyw_translation_article_model');
        $id_arr = $this->Lyw_translation_article_model->getIdListByUserid($_SESSION['user_id']);
        if (empty($id_arr)) {
            exit(json_encode(array('list' => array(), 'count_all' => 0)));
        }
        $id_str = arrToStrById($id_arr);
        $this->load->model('Lyw_article_manager_model');
        $manager_list = $this->Lyw_article_manager_model->getTranslationOurselfTask($id_str, $page, $rows);
        $this->lyw_common_model->addInfoToManagerList($manager_list);
        $this->Lyw_article_manager_model->getFinishListInfo($manager_list);
        $countAll = $this->Lyw_article_manager_model->getTranslationOurselfTaskCount($id_str);
        exit(json_encode(array('list' => $manager_list, 'count_all' => $countAll)));
    }


    public function save()
    {
        Permission::yy(1);
        $text = isset($_POST['text']) ? $_POST['text'] : '';
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        $title = isset($_POST['title']) ? $_POST['title'] : '';
        $manager_id = isset($_POST['manager_id']) ? $_POST['manager_id'] : 0;
        $translation_id = isset($_POST['translation_id']) ? $_POST['translation_id'] : 0;
        $is_received = isset($_POST['is_received']) ? $_POST['is_received'] : 0;
        $original_user_id = isset($_POST['original_user_id']) ? $_POST['original_user_id'] : 0;
        $status = isset($_POST['status']) ? $_POST['status'] : '';
        if (empty($text) || empty($manager_id) || empty($title)) {
            exit('Error : 信息不全!');
        }
        $this->lyw_common_model->translationSavePermission($manager_id, $translation_id, $original_user_id);
        $this->load->model('lyw_translation_article_model');
        if ($this->lyw_translation_article_model->saveAdd($translation_id, $manager_id, $_SESSION['user_id'], $title, $text, $html, $status)) {
            if ($is_received != 2) {
                $this->Lyw_article_manager_model->changeStatus($manager_id, 'is_received', 2);//将领取状态更改为不可修改
            }
            if ($status == 2) {
                //翻译员发布了内容,立即将内容增加到校对表内
                $this->load->model('Lyw_proofread_article_model');
                $proofread_article_id = $this->Lyw_proofread_article_model->add($original_user_id, $title, $text, $html);
                $this->Lyw_article_manager_model->changeStatus($manager_id, 'proofread_article_id', $proofread_article_id);//添加校对表id
            }
            exit(json_encode('Success : 保存成功!'));
        } else {
            exit(json_encode('Error : failure 保存数据库失败!'));
        }
    }
}